package com.qingcheng.controller;

import com.qingcheng.domain.ParaDto;
import com.qingcheng.entity.PageResult;
import com.qingcheng.entity.Result;
import com.qingcheng.pojo.goods.Para;
import com.qingcheng.pojo.goods.TbPara;
import com.qingcheng.service.ParaService;
import com.qingcheng.service.TbParaService;
import com.qingcheng.util.PageUtil;
import com.qingcheng.util.YouthR;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

@Slf4j
@RestController
@RequestMapping("/para")
@Api(tags = "TbParaController")
public class ParaController {

    @Autowired
    private ParaService paraService;

    @Resource
    private TbParaService tbParaService;

    @ApiOperation(value = "根据id获取信息")
    @GetMapping("/detail/{id}")
    public YouthR detail(@PathVariable Integer id) {

        TbPara tbPara = tbParaService.findById(id);
        return YouthR.ok().data("tbPara",tbPara);
    }

    @ApiOperation(value = "商品新增时更具templateID获取信息（list不分页）")
    @GetMapping("/list/{templateId}")
    public YouthR list(@PathVariable Integer templateId) {
        List<TbPara>  list = tbParaService.queryParaByTemId(templateId);
        return YouthR.ok().data("list",list);
    }



    @ApiOperation(value = "获取信息（list分页）不带条件")
    @GetMapping("/listPages")
    public YouthR listPages(Integer pageNum,Integer pageSize) {
        PageUtil<TbPara> pageUtils = tbParaService.findListAll(pageNum, pageSize);

        return YouthR.ok().data("pageUtils",pageUtils);
    }

    @ApiOperation(value = "获取信息（list分页）带条件")
    @PostMapping("pageQueryByAll/{pageNum}/{pageSize}")
    public YouthR pageQueryByAll(@PathVariable Integer pageNum,
                                 @PathVariable Integer pageSize,
                                 @RequestBody TbPara tbPara){
        PageUtil<ParaDto> pageUtils = tbParaService.PageQueryList(pageNum, pageSize, tbPara);
        return YouthR.ok().data("pageUtils",pageUtils);
    }

    @ApiOperation(value = "添加信息")
    @PostMapping("/addPara")
    public YouthR addPara(@RequestBody TbPara tbPara) {

        boolean b = tbParaService.saveTbPara(tbPara);
        if(b){
            return YouthR.ok();
        }
        return YouthR.error();
    }

    @ApiOperation(value = "修改信息")
    @PutMapping("/update")
    public YouthR update(@RequestBody TbPara tbPara) {
        boolean b = tbParaService.updateTbPara(tbPara);

        if(b){
            return YouthR.ok();
        }
        return YouthR.error();
    }

    @ApiOperation(value = "删除信息（主键）")
    @DeleteMapping("/delete/{id}")
    public YouthR delete(@PathVariable Integer id) {
        boolean b = tbParaService.deleteById(id);
        if(b){
            return YouthR.ok();
        }
        return YouthR.error();
    }

    @GetMapping("/findAll")
    public List<Para> findAll(){
        return paraService.findAll();
    }

    @GetMapping("/findPage")
    public PageResult<Para> findPage(int page, int size){
        return paraService.findPage(page, size);
    }

    @PostMapping("/findList")
    public List<Para> findList(@RequestBody Map<String,Object> searchMap){
        return paraService.findList(searchMap);
    }

    @PostMapping("/findPage")
    public PageResult<Para> findPage(@RequestBody Map<String,Object> searchMap,int page, int size){
        return  paraService.findPage(searchMap,page,size);
    }

    @GetMapping("/findById")
    public Para findById(Integer id){
        return paraService.findById(id);
    }


}
